import React, { Component } from 'react'
import './index.css'

export default class Item extends Component {
  state = {
    mouse: false
  }

  // 设置鼠标悬停效果
  handleMouse=(flag)=>{
    return ()=>{
      this.setState({mouse: flag})
    }
  }

  // 设置勾选状态
  handleCheck=(id)=>{
    return (event)=>{
      this.props.updateTodo(id, event.target.checked)
    }
  }

  // 设置删除TODO
  handleDelete = (id)=>{
    if(window.confirm("确定删除吗？")){
      this.props.deleteTodo(id)
    }
    return
  }

  // 设置
  render() {
    const {id, name, done} = this.props
    const {mouse} = this.state
    return (
      <li 
      style={{backgroundColor: mouse ? "#ddd" : "white"}}
      onMouseEnter={this.handleMouse(true)} 
      onMouseLeave={this.handleMouse(false)}>
        <label>
          <input type="checkbox" checked={done} onChange={this.handleCheck(id)}/>
          <span>{name}</span>
        </label>
        <button 
        className="btn btn-danger" 
        style={{display: mouse ? "block" :'none'}}
        onClick={()=>{this.handleDelete(id)}}
        >删除</button>
      </li>
    )
  }
}
